//
//  CalculatorTableViewController.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/7/7.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "CalculatorTableViewController.h"
#import "CalculatorBTableViewCell.h"


@interface CalculatorTableViewController ()

@property (nonatomic, strong)NSMutableArray *tbCellArray;

@end

@implementation CalculatorTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tbCellArray = [NSMutableArray arrayWithCapacity:2];
    NSArray *cellArrayName = @[@"CalculatorATableViewCell",@"CalculatorBTableViewCell"];
    
    for (int i = 0; i < [cellArrayName count]; i++) {
        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[cellArrayName objectAtIndex:i]];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:[cellArrayName objectAtIndex:i] owner:self options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            if ([[cellArrayName objectAtIndex:i] isEqualToString:@"CalculatorATableViewCell"]) {
                if ([self.housePrice length]) {
                    ((CalculatorATableViewCell*)cell).housePrice.text = self.housePrice;
                    
                }
                ((CalculatorATableViewCell*)cell).delegate = self;
            }
        }
        [self.tbCellArray addObject:cell];
    }
    
    CalculatorATableViewCell *cell = [self.tbCellArray objectAtIndex:0];
    [cell calculatorAction];

}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self hiddenKeyBoard];
}

- (void)hiddenKeyBoard
{
    CalculatorATableViewCell *cellA = [self.tbCellArray objectAtIndex:0];
    [cellA.housePrice resignFirstResponder];
    [cellA.firstPrice resignFirstResponder];
    [cellA.loanYears resignFirstResponder];
    [cellA.lendRate resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.tbCellArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tbCellArray objectAtIndex:indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ((UITableViewCell*)[self.tbCellArray objectAtIndex:indexPath.row]).frame.size.height;
}

- (void)calculatorResultWithTotalLoanPrice:(NSString *)loanPrice monthlyPayment:(NSString *)mothlyPayment andTotalInterest:(NSString *)totalInterest
{
    [self hiddenKeyBoard];
    CalculatorBTableViewCell *cell = [self.tbCellArray lastObject];
    cell.lendPrice.text = loanPrice;
    cell.returnOfMonth.text = mothlyPayment;
    cell.rateCountPrice.text = totalInterest;
}



@end
